<?php
	class Test_ExerciceManager{
		
		  private $_db;
	
		  public function __construct($db){
				$this->setDb($db);
		  }
		  
		  public function setDb(PDO $db){
				$this->_db = $db;
		  }	
		  
		  public function add(Test_Exercice $test_exercice){// permet d'ajouter un test_exercice
				$q = $this->_db->prepare('INSERT INTO test_exercice SET idRefTest = :idRefTest, idRefExercice = :idRefExercice, ordre = :ordre');
				$p = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$test_exercice->idRefTest());
				
				$q->bindValue(':idRefTest', $test_exercice->idRefTest());
				$q->bindValue(':idRefExercice', $test_exercice->idRefExercice());
				
				if(is_nan($p->rowCount())){
					$ordre = 0;
				} else {
					$ordre = $p->rowCount()+1;
				}
				
				$q->bindValue(':ordre', $ordre);
				$q->execute();
		  }		
		    		
		  public function delete(Test_Exercice $test_exercice){// permet de supprimer un test
			  $this->_db->exec('DELETE FROM test_exercice WHERE idRefTest = '.$test_exercice->idRefTest().' AND idRefExercice = '.$test_exercice->idRefExercice());
		  }
		  
		  public function update(Test_Exercice $test_exercice){// permet de modifier un test
				$q = $this->_db->prepare('UPDATE test_exercice SET ordre = :ordre WHERE idRefTest = '.$test_exercice->idRefTest().' AND idRefExercice = '.$test_exercice->idRefExercice());
				$q->bindValue(':ordre', $test_exercice->ordre());
				$q->execute();
		  }
		  
		  public function get($idRefTest, $idRefExercice){// retourne l'objet test d'id correspondant
				$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idRefTest.' AND idRefExercice = '.$idRefExercice);
				$donnees = $q->fetch();
				return new Test_Exercice($donnees);
		  }
		  
		  public function getByTest($idRefTest){ //Retourne un tableau d'objet Test_Exercice en fonction du test
				$testex = array();
				$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idRefTest.' ORDER BY ordre');
				while ($donnees = $q->fetch()){
					$testex[] = new Test_Exercice($donnees);
				}
				return $testex;			  
		  }
		  
		public function ordreUp($idRefTest, $idRefExercice) { //Modifie l'ordre d'un exercice dans un test (+1)
			$test_exercice = $this->get($idRefTest, $idRefExercice);
			$ordreM = $test_exercice->ordre() - 1;
			$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idRefTest.' AND ordre = '.$ordreM);
			
			if($q->rowCount() == 1)
			{
				$test_exercice2 = new Test_Exercice($q->fetch());
				$test_exercice2->setOrdre($test_exercice2->ordre()+1);
				$test_exercice->setOrdre($test_exercice->ordre()-1);
				$this->update($test_exercice);
				$this->update($test_exercice2);
			}
		}
		
		public function ordreDown($idRefTest, $idRefExercice) { //Modifie l'ordre d'un exercice dans un test (-1)
			$test_exercice = $this->get($idRefTest, $idRefExercice);
			$ordreM = $test_exercice->ordre() + 1;
			$q = $this->_db->query('SELECT * FROM test_exercice WHERE idRefTest = '.$idRefTest.' AND ordre = '.$ordreM);

			if($q->rowCount() == 1)
			{
				$test_exercice2 = new Test_Exercice($q->fetch());
				$test_exercice2->setOrdre($test_exercice2->ordre()-1);
				$test_exercice->setOrdre($test_exercice->ordre()+1);
				$this->update($test_exercice);
				$this->update($test_exercice2);
			}
		}
	}
?>